const postekPrinter = uni.requireNativePlugin('PostekPrinter');

export default {
  // 连接网络打印机
  connectNetworkPrinter(ip, port = 9100, timeout = 3000) {
    return new Promise((resolve, reject) => {
      postekPrinter.connectNetworkPrinter({
        ip: ip,
        port: port,
        timeout: timeout
      }, (result) => {
        if (result.success) {
          resolve(result);
        } else {
          reject(result.error || '连接打印机失败');
        }
      });
    });
  },

  // 打印文本
  printText(x, y, content, options = {}) {
    const {
      direction = 0,
      font = 2,
      horizontal = 1,
      vertical = 1,
      textType = 'N'
    } = options;

    return new Promise((resolve, reject) => {
      postekPrinter.printText({
        x: x,
        y: y,
        direction: direction,
        font: font.charCodeAt(0),
        horizontal: horizontal,
        vertical: vertical,
        textType: textType.charCodeAt(0),
        content: content
      }, (result) => {
        if (result.success) {
          resolve(result);
        } else {
          reject(result.error || '打印文本失败');
        }
      });
    });
  },

  // 打印标签
  printLabel(number = 1, copies = 1) {
    return new Promise((resolve, reject) => {
      postekPrinter.printLabel({
        number: number,
        copies: copies
      }, (result) => {
        if (result.success) {
          resolve(result);
        } else {
          reject(result.error || '打印标签失败');
        }
      });
    });
  },

  // 断开打印机连接
  disconnectPrinter() {
    return new Promise((resolve, reject) => {
      postekPrinter.disconnectPrinter((result) => {
        if (result.success) {
          resolve(result);
        } else {
          reject(result.error || '断开打印机连接失败');
        }
      });
    });
  }
};